package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import dao.Connector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Trung
 */
public class RoomType {
        Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tbltyperoom";
	
	public boolean addType(String name, int cost, String info){
		connect = Connector.getConnect();
		if (connect == null)
			System.out.println("null");
		try{
			pstm= connect.prepareStatement("INSERT INTO "+ table+"(name,cost,info) VALUES (?,?,?)");
			pstm.setString(1,name);
			pstm.setInt(2, cost);
			pstm.setString(3, info);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
	}
	
	public boolean deleteType(int id){
		connect = Connector.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}
	public boolean updateType(int id, String name,int cost,String info){
		connect= Connector.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +" SET name=?," +
					"cost=?,info=?  WHERE id=?");
			pstm.setString(1, name);
			pstm.setInt(2,cost);
			pstm.setString(3,info);
                        pstm.setInt(4,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}
	public  ArrayList<entity.RoomType> getAllType(){
		connect= Connector.getConnect();
		ArrayList<entity.RoomType> list = new ArrayList<entity.RoomType>();		
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);			
			rs=pstm.executeQuery();			
			while(rs.next()){
				entity.RoomType room = new entity.RoomType();
				room.setId(rs.getInt("id"));
				room.setName(rs.getString("name"));
                                room.setCost(rs.getInt("cost"));
				room.setInfo(rs.getString("info"));
				list.add(room);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}
	public entity.RoomType getTypeRoomById(int id){
		
		connect= Connector.getConnect();
		ResultSet rs=null;
		entity.RoomType type = null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
		while(rs.next()){
			type=  new entity.RoomType();;
			type.setId(rs.getInt("id"));
			type.setName(rs.getString("name"));
			type.setCost(rs.getInt("cost"));
			type.setInfo(rs.getString("info"));                        
		}
	
		}catch(SQLException e){
			e.printStackTrace();
		}
		return type;
	}
   
        
}
